package com.xnf.ssmweb.dao;

import com.xnf.ssmweb.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chendx on 2021/3/30.
 */
@Repository
public interface UserInfoMapper {

    @Select("select * from users")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "email" ,property = "email"),
            @Result(column = "username" ,property = "username"),
            @Result(column = "password" ,property = "password"),
            @Result(column = "phoneNum" ,property = "phoneNum"),
            @Result(column = "status" ,property = "status"),
            @Result(column = "id",property = "roles",javaType = List.class,
            many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByUserId"))
    })
    List<UserInfo> findAll();

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "email" ,property = "email"),
            @Result(column = "username" ,property = "username"),
            @Result(column = "password" ,property = "password"),
            @Result(column = "phoneNum" ,property = "phoneNum"),
            @Result(column = "status" ,property = "status"),
            @Result(column = "id",property = "roles",javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByUserId"))
    })
    UserInfo findById(Long id);


    @Select("select * from users where id in (select userId from users_role where roleId = #{roleId})")
    List<UserInfo> findUserByRoleId(Long roleId);

    @Insert("insert into users (email,username,password,phoneNum,status) " +
            "values (#{email},#{username},#{password},#{phoneNum},#{status})")
    int save(UserInfo userInfo);

    @Insert("insert into users_role (userId,roleId) values(#{userId},#{roleId})")
    int addRoleToUser(@Param("userId") Long userId,@Param("roleId") Long roleId);

    @Select("select * from users where username = #{username}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "email" ,property = "email"),
            @Result(column = "username" ,property = "username"),
            @Result(column = "password" ,property = "password"),
            @Result(column = "phoneNum" ,property = "phoneNum"),
            @Result(column = "status" ,property = "status"),
            @Result(column = "id",property = "roles",javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByUserId"))
    })
    UserInfo findByUsername(String username);
}
